package cn.kgc.ssm.controller;


import cn.kgc.ssm.pojo.BackendUser;
import cn.kgc.ssm.service.BackendUserService;
import cn.kgc.ssm.tools.Constants;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("manager")
@Controller
public class UserLoginController {
	private Logger logger = Logger.getLogger(UserLoginController.class);
	
	@Resource
	private BackendUserService backendUserService;

	@RequestMapping("login")
	public  String login(){
		return "backendlogin";
	}

	@RequestMapping("dologin")
	public String doLogin(String userCode,String userPassword,Model model, HttpSession session) {
		//调用service方法，进行用户匹配
		BackendUser user = null;
		try {
			user = backendUserService.login(userCode, userPassword);
			if (null != user) {//登录成功
				//放Session
				session.setAttribute(Constants.USER_SESSION, user);
				return "redirect:/manager/backend/main";
			} else {
				model.addAttribute("error", "用户名或密码不正确");
				return "backendlogin";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return " " ;
	}

	@RequestMapping("backend/main")
	public String main(){
		return "backend/main";
	}


}
